package com.sokal.easycare.modules.kpoint.web.after;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.kpoint.entity.after.KPointAftermarket;
import com.sokal.easycare.modules.kpoint.service.after.KPointAftermarketService;

/**
 * 维修保养点Controller
 * @author lihaipei
 * @version 2017-05-17
 */
@Controller
@RequestMapping(value = "${adminPath}/kpoint/after/kPointAftermarket")
public class KPointAftermarketController extends BaseController {

	@Autowired
	private KPointAftermarketService kPointAftermarketService;
	
	@ModelAttribute
	public KPointAftermarket get(@RequestParam(required=false) String id) {
		KPointAftermarket entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kPointAftermarketService.get(id);
		}
		if (entity == null){
			entity = new KPointAftermarket();
		}
		return entity;
	}
	
	@RequiresPermissions("kpoint:after:kPointAftermarket:view")
	@RequestMapping(value = {"list", ""})
	public String list(KPointAftermarket kPointAftermarket, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KPointAftermarket> page = kPointAftermarketService.findPage(new Page<KPointAftermarket>(request, response), kPointAftermarket); 
		model.addAttribute("page", page);
		model.addAttribute("kPointAftermarket", kPointAftermarket);
		return "modules/kpoint/after/kPointAftermarketList";
	}

	@RequiresPermissions("kpoint:after:kPointAftermarket:view")
	@RequestMapping(value = "form")
	public String form(KPointAftermarket kPointAftermarket, Model model) {
		model.addAttribute("kPointAftermarket", kPointAftermarket);
		return "modules/kpoint/after/kPointAftermarketForm";
	}

	@RequiresPermissions("kpoint:after:kPointAftermarket:edit")
	@RequestMapping(value = "save")
	public String save(KPointAftermarket kPointAftermarket, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kPointAftermarket)){
			return form(kPointAftermarket, model);
		}
		kPointAftermarketService.save(kPointAftermarket);
		addMessage(redirectAttributes, "保存维修保养点成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/after/kPointAftermarket/?repage";
	}
	
	@RequiresPermissions("kpoint:after:kPointAftermarket:edit")
	@RequestMapping(value = "delete")
	public String delete(KPointAftermarket kPointAftermarket, RedirectAttributes redirectAttributes) {
		kPointAftermarketService.delete(kPointAftermarket);
		addMessage(redirectAttributes, "删除维修保养点成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/after/kPointAftermarket/?repage";
	}

}